
# 克隆 hzero-front 仓库代码
echo ">>> 克隆 hzero-front 仓库代码"
git clone https://gitee.com/open-hand/hzero-front.git

cd hzero-front
rm -rf dist

# 安装本项目packages依赖(即初始化workspace)
echo ">>> 安装项目packages依赖：lerna bootstrap"
lerna bootstrap --registry http://nexus.saas.hand-china.com/content/groups/hzero-npm-group/

# hzero-front主体工程依赖于packages下的各模块,所以需要编译packages下的各模块
echo ">>> 编译packages下的各模块：lerna bootstrap"
yarn run transpile

# 跳过下载Chromium
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

# 编译生产环境包（带变量）
echo ">>> 编译生产环境包：yarn build:production"
yarn build:production
buildErrorCode=$?
if [ 0 -ne $buildErrorCode ]; then
  echo $buildErrorCode
  echo 'build error';
  exit $buildErrorCode;
fi

cp -r dist ../
cd ..

# 替换编译包中的环境变量值
echo ">>> 替换环境变量值"
find dist -name '*.js' | xargs sed -i "s BUILD_BASE_PATH / g"
find dist -name '*.js' | xargs sed -i "s BUILD_API_HOST http://dev.hzero.org:8080 g"
find dist -name '*.js' | xargs sed -i "s BUILD_CLIENT_ID hzero-front-dev g"
find dist -name '*.js' | xargs sed -i "s BUILD_WEBSOCKET_HOST http://dev.hzero.org:8080/hpfm/sock-js g"
find dist -name '*.js' | xargs sed -i "s BUILD_CUSTOMIZE_ICON_NAME customize-icon g"
find dist -name '*.js' | xargs sed -i "s BUILD_MULTI_SKIN_ENABLE true g"
find dist -name '*.js' | xargs sed -i "s BUILD_TOP_MENU_UNION_LABEL true g"
find dist -name '*.js' | xargs sed -i "s BUILD_TOP_MENU_LABELS HZERO_MENU g"
find dist -name '*.js' | xargs sed -i "s BUILD_ENV_SIGN 'HZERO技术平台' g"

# 拷贝编译后文件到nginx运行目录
echo ">>> 运行 Nginx 服务"
rm -rf /usr/share/nginx/html
mv dist /usr/share/nginx/html

service nginx restart

echo ">>> File parsing successful
============================================================================
Please visit ---> http://$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:')
============================================================================"